using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using PickGold.Charting.ChartTypes;

namespace PickGold.Charting.Design
{
	internal class ChartTypeEditor : UITypeEditor
	{
		private ChartTypeRegistry _chartTypeRegistry;

		public override bool GetPaintValueSupported(ITypeDescriptorContext context)
		{
			if ((context != null) && (context.Instance != null))
			{
				IChartElement instance = context.Instance as IChartElement;
				if (instance != null)
				{
					this._chartTypeRegistry = instance.Common.ChartTypeRegistry;
				}
			}
			return true;
		}

		public override void PaintValue(PaintValueEventArgs e)
		{
			string chartTypeName = string.Empty;
			if ((this._chartTypeRegistry != null) && (e != null))
			{
				if (e.Value is string)
				{
					chartTypeName = (string)e.Value;
				}
				else if (e.Value is SeriesChartType)
				{
					chartTypeName = Series.GetChartTypeName((SeriesChartType)e.Value);
				}
				if (!string.IsNullOrEmpty(chartTypeName))
				{
					IChartType chartType = this._chartTypeRegistry.GetChartType(chartTypeName);
					Image image = null;
					if (chartType != null)
					{
						image = chartType.GetImage(this._chartTypeRegistry);
					}
					if (image != null)
					{
						e.Graphics.DrawImage(image, e.Bounds);
					}
				}
			}
		}
	}
}

